/**
 * GenericDaoImp.java
 *
 *
 * $LastChangedBy: allen $
 * $LastChangedDate: 2007-12-07 15:48:34 +0800 $
 * $Revision: 1399 $
 */
package com.myspace.dao.hibernate;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.myspace.dao.GenericDao;
import com.myspace.domain.DomainObject;

/**
 * Implements the GenericDao by using Spring APIs.
 *
 */
public abstract class GenericDaoImp<T extends DomainObject, ID extends Serializable>
		extends HibernateDaoSupport implements GenericDao<T, ID> {

	private Class<T> persistenceClass;

	@SuppressWarnings("unchecked")
	public GenericDaoImp() {
		this.persistenceClass = (Class<T>) ((ParameterizedType) this.getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
	}

	@SuppressWarnings("unchecked")
	public T findById(ID id) {
		T entity = (T) getHibernateTemplate().get(getPersistenceClass(), id);
		return entity;
	}

	public T save(T entity) {
		getHibernateTemplate().save(entity);
		return entity;
	}

	public void delete(T entity) {
		getHibernateTemplate().delete(entity);
	}

	public T update(T entity) {
		getHibernateTemplate().update(entity);
		return entity;
	}

	public Class<T> getPersistenceClass() {
		return persistenceClass;
	}

}
